/**
 *@Title: IpicTargetServiceImpl.java  
 * @author: 胡彪
 * @date: 2013-5-29 下午03:43:01
 * @Description:
 * @version: V1.0   
 */
package com.imonsoft.sqm.service.basedata;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.dao.basedata.IpicTargetDAO;
import com.imonsoft.sqm.pojo.basedata.IpicTarget;
import com.imonsoft.sqm.pojo.basedata.IpicTargetExample;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.web.excel.IpicTargetExcel.IpicTargetImportDto;
import org.springframework.util.StringUtils;

/**
 *
 *
 */
@Service("ipicTargetService")
public class IpicTargetServiceImpl implements IpicTargetService {

	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private IpicTargetDAO ipicTargetDAO;
	@Autowired
	private SupplierService supplierService;

	public int delete(Long id) {
		return 0;
	}

	@Override
	public List<IpicTarget> findList(IpicTarget entity) {
		IpicTargetExample example = new IpicTargetExample();
		IpicTargetExample.Criteria criteria = example.createCriteria();
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		criteria.andYearEqualTo(entity.getYear());
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
		return ipicTargetDAO.selectIpicTargetByExample(example);
	}

	@Override
	public IpicTarget get(Long id) {
		return ipicTargetDAO.selectIpicTargetByPrimaryKey(id);
	}

	@Override
	public DataGrid<IpicTarget> getDataGrid(IpicTarget entity, Pager pager) {

		IpicTargetExample example = new IpicTargetExample();
		IpicTargetExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);

		if (null != entity) {
			if (entity.getSupplierId()!=null)
				criteria.andSupplierIdEqualTo(entity.getSupplierId());
			// 供应商编码
			if (!StringUtil.isBlank(entity.getSupplierCode()))
				criteria.andSupplierCodeLike(entity.getSupplierCode() + "%");
			// 供应商名称
			if (!StringUtil.isBlank(entity.getSupplierName()))
				criteria.andSupplierNameLike(entity.getSupplierName() + "%");
			// 年份
			if (!StringUtil.isBlank(entity.getYear()))
				criteria.andYearLike(entity.getYear() + "%");
		}

		return ipicTargetDAO.getDataGrid(example, pager);
	}

	@Override
	public Long insert(IpicTarget entity)    {
		entity.setState(ConstantSqm.STATUS_NORMAL);
		List<IpicTarget> list = findListByState(entity);
		if(list!=null&&list.size()>0){
			IpicTarget bean=list.get(0);
			bean.setValue(entity.getValue());
			bean.setState(ConstantSqm.STATUS_NORMAL);
		    ipicTargetDAO.updateIpicTargetByPrimaryKey(bean);
			return null;
		}
		
	
		return ipicTargetDAO.insertIpicTarget(entity);
	}

	@Override
	public int update(IpicTarget entity) {
		
		IpicTarget ipicTarget = ipicTargetDAO.selectIpicTargetByPrimaryKey(entity.getId());
		Supplier supplier = supplierService.get(Long.valueOf(entity.getSupplierId()));
		ipicTarget.setSupplierId(entity.getSupplierId());
		ipicTarget.setSupplierName(supplier.getName());
		ipicTarget.setSupplierCode(supplier.getCode());
		ipicTarget.setYear(entity.getYear());
		ipicTarget.setValue(entity.getValue());
		return ipicTargetDAO.updateIpicTargetByPrimaryKey(ipicTarget);
	}

	@Override
	public void deleteIpicTarget(List<Long> idList) {

		if(idList!=null&&!idList.isEmpty()){
			for (int i = 0; i < idList.size(); i++) {
				IpicTarget ipicTarget = ipicTargetDAO.selectIpicTargetByPrimaryKey(idList.get(i));
				ipicTarget.setState(ConstantSqm.STATUS_DELETE);
				ipicTargetDAO.updateIpicTargetByPrimaryKey(ipicTarget);
			}
		}

	}

    @Override
    public Double getIpicTargetValue(Map<String, Object> searchMap) {
        IpicTargetExample example = new IpicTargetExample();
        IpicTargetExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(null!=searchMap){
            if(null!=searchMap.get("supplierId")){
                criteria.andSupplierIdEqualTo((Long)searchMap.get("supplierId"));
            }
            if(null!=searchMap.get("year")){
                criteria.andYearEqualTo((String)searchMap.get("year"));
            }
        }
        List<IpicTarget> ipicTargetList = ipicTargetDAO.selectIpicTargetByExample(example);
        double value=0.0;
        if(ipicTargetList!=null && ipicTargetList.size()==1){
            if(null!=ipicTargetList.get(0).getValue()){
                value=ipicTargetList.get(0).getValue();
            }
        }
        return value;
    }
    @Override
    public Double getIpicTargetValue(Long supplierId,String year) {
        IpicTargetExample example = new IpicTargetExample();
        IpicTargetExample.Criteria criteria = example.createCriteria();
        if(null==supplierId || StringUtils.isEmpty(year)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        criteria.andSupplierIdEqualTo(supplierId);
        criteria.andYearEqualTo(year);

        List<IpicTarget> ipicTargetList = ipicTargetDAO.selectIpicTargetByExample(example);
        double value=0.0;
        if(ipicTargetList!=null && ipicTargetList.size()==1){
            if(null!=ipicTargetList.get(0).getValue()){
                value=ipicTargetList.get(0).getValue();
            }
        }
        return value;
    }

    public List<IpicTarget> findListByState(IpicTarget entity) {
		IpicTargetExample example = new IpicTargetExample();
		IpicTargetExample.Criteria criteria = example.createCriteria();
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		criteria.andYearEqualTo(entity.getYear());
		return ipicTargetDAO.selectIpicTargetByExample(example);
	}
	/**
	 * 查询所有
	 */
	@Override
	public List<IpicTarget> queryAll() {
		IpicTargetExample example = new IpicTargetExample();
		List<IpicTarget> ipicTargetList = ipicTargetDAO.selectIpicTargetByExample(example);
		return ipicTargetList;
	}
	/**
	 * zhouwei 系统导入保存
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void txInsertForSys(List<IpicTargetImportDto> importList) {
		if (null == importList) {
			throw new SystemException("提交数据不能为空");
		}
		if (importList != null && importList.size() > 0) {
			for (IpicTargetImportDto s : importList) {
				IpicTarget ipicTarget = new IpicTarget();
				ipicTarget.setSupplierCode(s.getSupplierCode());
				ipicTarget.setYear(s.getYear());
				List<Supplier> supplierList=supplierService.findSupplierByCode(s.getSupplierCode());
				if(supplierList!=null&&supplierList.size()>0){
					ipicTarget.setSupplierId(supplierList.get(0).getId());
					ipicTarget.setSupplierName(supplierList.get(0).getName());
				}
				List<IpicTarget> checkHave = findList(ipicTarget);
				if (checkHave != null && checkHave.size() > 0) {
					throw new SystemException(checkHave.get(0).getSupplierName()+checkHave.get(0).getYear()+"的IPIC已经存在");
				}
				BeanUtils.copyProperties(s, ipicTarget);
				ipicTarget.setState(ConstantSqm.STATUS_NORMAL);
				ipicTargetDAO.insertIpicTarget(ipicTarget);
			}
		}
	}
}
